<template>
  <el-dialog width="50%" title="修改订单数据" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <el-form class="ruleForm" :model="form" label-width="100px">
      <el-form-item v-if="form.num == 2" label="租赁方式" prop="pact_type">
        <el-radio-group v-model="form.pact_type">
          <el-radio v-for="(val, key) in initData.pact_type" :key="key" :label="key">{{ val }}</el-radio>
        </el-radio-group>
        <p class="type_tip">
          <template v-if="form.pact_type == 1">
            <span>固定租期的芝麻信用授权可扣款金额=商品套餐押金+总租金</span><br>商品设置固定租期后，承租人须待租赁期满后才能退还租赁物。固定租期无法使用花呗分期功能。
          </template>
          <template v-if="form.pact_type == 2">
            <span>租完即送的芝麻信用授权可扣款金额=总租金</span><br>商品设置租完即送后，租赁期内承租人不得单方面解除协议提前退还租赁物，承租人须按订单约定按期支付租金。租完即送可使用花呗分期功能。
          </template>
          <template v-if="form.pact_type == 3">
            <span>随租随还的芝麻信用授权可扣款金额=商品套餐押金+总租金</span><br>商品设置随租随还后，承租人可以随时退还租赁物。随租随还无法使用花呗分期功能。
          </template>
        </p>
      </el-form-item>
      <el-form-item v-if="form.num == 2 && form.pact_type == 1" label="每期租金">
        <el-input v-model="form.rent" clearable placeholder="请输入每期租金" />
      </el-form-item>
      <el-form-item v-if="form.num == 2" label="市场价">
        <el-input v-model="form.setmeal_foregift" clearable placeholder="请输入市场价" />
		<p class="type_tip">
			<span>市场价=订单总价值(总租金+买断金)，非必要情况下请勿增加市场价金额，否则会导致代扣金额超出预授权金额造成部分租金/买断金代扣失败。</span>
		</p>
      </el-form-item>
	  <el-form-item v-if="form.num == 2" label="租期" prop="month_total">
	    <el-radio-group v-model="form.month_total">
	      <el-radio v-for="(val, key) in initData.month_total" :key="key" :label="key">{{ val }}</el-radio>
	    </el-radio-group>
	  </el-form-item>
      <el-form-item v-if="form.num == 2" label="是否参与活动">
        <el-radio-group v-model="form.activity_id">
          <el-radio v-for="(val, key) in initData.activity" :key="key" :label="key">{{ val }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.num == 3" label="买断金">
        <el-input v-model="form.setmeal_price" clearable placeholder="请输入买断金" />
      </el-form-item>
      <el-form-item v-if="form.num == 4" label="供应商">
        <el-select v-model="form.shopname" filterable placeholder="请选择供应商" @change="changeSellerId">
          <el-option v-for="item in shopname_data" :key="item.shopname" :label="item.shopname" :value="item.member_id" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.num == 1" label="分配供应商">
        <el-select v-model="form.member_id" filterable placeholder="请选择供应商" multiple>
          <el-option v-for="item in shopname_data" :key="item.shopname" :label="item.shopname" :value="item.member_id" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.num == 5" label="市场价">
        <el-input v-model="form.setmeal_foregift_apply" clearable placeholder="输入活动后市场价" />
        <p class="type_tip">
          活动前市场价：{{ form.setmeal_foregift }}
        </p>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOrderData, editOrderData, getShopUser, getLeaseRent } from '@/api/order'
export default {
  // props: ['editItem'],
  props: {
    editItem: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      form: {
        orderId: '',
        pact_type: '',
		month_total:'',
        setmeal_price: '',
        setmeal_foregift_apply: '',
        shopname: '',
        seller_id: '', // 供应商商户ID
        rent: '', // 每期租金
		setmeal_foregift:'', //市场价
		activity_id:'',//活动ID
		member_id:[], //分配供应商ID
        num: '' // 1 租赁方式  2 每期租金 3 买断金 4 供应商
      },
      initData: {
        pact_type: [], // 租赁方式
		month_total: [],//租期
		activity:[],//是否参加活动
        setmeal_price: '', // 买断金
        shopname: '', // 供应商
        rent: '',// 每期租金
		setmeal_foregift: '', //市场价
        setmeal_foregift_apply: '' //更新后的预授权金额
      },
      shopname_data: [], // 供应商列表
      dialogVisible: false
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.init()
        this.getData()
      }
    }
  },
  methods: {
    getData() {
      this.initData.setmeal_price = this.form.setmeal_price
      this.initData.shopname = this.form.shopname
      getOrderData(this.form.orderId).then(res => {
        this.initData.pact_type = res.pact_type
        this.initData.month_total = res.month_total
        this.initData.activity = res.activity
      })
      getShopUser().then(res => {
        this.shopname_data = res
      })
      getLeaseRent(this.form.orderId).then(res => {
        this.form.rent = res.rent
      })
    },
    init() {
      for (const key in this.form) {
        this.form[key] = this.editItem[key].toString() || ''
      }
    },
    changeSellerId(item) {
      this.form.seller_id = item
    },
    save() {
      const that = this
      this.$confirm('确定修改？', '修改商品', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        const data = { ...that.form }
        // console.log(data);
        editOrderData(data).then(res => {
          this.dialogVisible = false
          this.$emit('getList', true)
        })
      }).catch(() => { })
    }
  }
}
</script>

<style lang="scss" scoped>
.type_tip {
  color: #999999;
  line-height: 22px;

  span {
    color: #ff4146;
  }
}
</style>
